﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*
 场景题：图形原型管理器
 场景描述：
     在你的图形设计应用程序中，需要一个机制来管理和创建各种形状对象，例如圆形、矩形和三角形。
     为了提高性能和效率，应用程序应该能够通过克隆现有形状对象来创建新的形状，而不是每次都从头开始创建。
     这就需要一个原型管理器，它可以存储已有的形状对象，并在需要时提供这些对象的克隆。
 */
namespace CS_BEGINNER_TO_PROFICIENT.DesignPatterns.CreationMode.PrototypePattern
{
    public class GraphicsPrototypeManager
    {
        private static readonly Dictionary<string, Shape> ShapeMap = new Dictionary<string, Shape>();

        public static Shape GetShape(string shapeId)
        {
            if (ShapeMap.ContainsKey(shapeId))
            {
                return ShapeMap[shapeId].Clone() as Shape;
            }
            return null;
        }

        public static void LocalCache()
        {
            Shape circle = new Circle();
            circle.Id = "1";
            ShapeMap.Add(circle.Id, circle);

            Shape retangle = new Rectangle();
            retangle.Id = "2";
            ShapeMap.Add(retangle.Id, retangle);

            Shape triangle = new Triangle();
            triangle.Id = "3";
            ShapeMap.Add(triangle.Id, triangle);
        }
    }

    public abstract class Shape : ICloneable
    {
        public string Id { get; set; }
        public string Type { get; protected set; }
        public abstract void Draw();
        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }

    public class Circle : Shape
    {
        public Circle() => Type = "Circle";

        public override void Draw() => Console.WriteLine("Drawing a Circle.");
    }

    public class Rectangle : Shape
    {
        public Rectangle() => Type = "Rectangle";

        public override void Draw() => Console.WriteLine("Drawing a Rectangle.");
    }

    public class Triangle : Shape
    {
        public Triangle() => Type = "Triangle";

        public override void Draw() => Console.WriteLine("Drawing a Triangle.");
    }
}
